import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import bundleManager from '@ohos.bundle.bundleManager';
import DeviceUtils from '../utils/DeviceUtils';
//loading
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
import { LoadingUtils } from '../utils/LoadingUtils'
//本地话
AppStorage.SetOrCreate("appName","AppStorage")

async function getBundleInfoForSelf(callback?: ((data: number)=>void)) {
  try {
    bundleManager.getBundleInfoForSelf(
      bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT |
      bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION |
      bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE |
      bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY,
      (err, data) => {
        if (!err) {
          EntryAbility.device.bundleInfo = data
        };
        if (callback != null) {
          callback(1)
        };
      });
  } catch (err) {
    if (callback != null) callback(-1);
  }
}


export default class EntryAbility extends UIAbility {

  public static  readonly device: DeviceUtils = new DeviceUtils();


  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.context.getApplicationContext().on('abilityLifecycle', this.abilityLifecycleCallback)
  }


  /**
   *  声明ability生命周期回调，需配置所有回调后才可以在applicationContext注册
   */
  abilityLifecycleCallback: AbilityLifecycleCallback = {
    onAbilityCreate(ability: UIAbility) {},

    onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage) {
      // 订阅子窗口事件
      LoadingUtils.subscribeLoadingEvent(ability, windowStage)
    },

    onWindowStageActive(ability: UIAbility, windowStage: window.WindowStage) {},

    onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage) {},

    onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage) {
      LoadingUtils.unsubscribeLoadingEvent(ability)
    },

    onAbilityDestroy(ability: UIAbility) {},

    onAbilityForeground(ability: UIAbility) {},

    onAbilityBackground(ability: UIAbility) {},

    onAbilityContinue(ability: UIAbility) {}
  }



  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {

    // 获取到 Window 后保存在 globalThis 中
    var mainWindow = windowStage.getMainWindowSync();
    globalThis.mainWindow = mainWindow;


    getBundleInfoForSelf()

    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });

    getBundleInfoForSelf()

  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
